<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Xml"%>

<%@ Import Namespace="PPAP.BO"%>

<script runat="server">
void Page_Load(Object sender,EventArgs e)
{
	XmlDocument iDoc = new XmlDocument();
	iDoc.Load(Request.InputStream);
	
//	var str = "<xml><MetaDirName>" + d + "</MetaDirName></xml>";

	string metaDirName = iDoc.DocumentElement.SelectSingleNode("//FBItem").InnerText;
	
	string sqlSelectList = "";
	
	System.Text.StringBuilder sb = new System.Text.StringBuilder();
	sb.Append("<xml>");
	sb.Append("<data>");

	sqlSelectList = " select FIID as [ID], FICode as [Code], FIName as [Name] from PDHRFBItem order by FICode" ;
	
	FBItemBO fbibo = new FBItemBO();
	DataTable t = fbibo.GetFBItemIDAndName( sqlSelectList ) ;
	
	string curLevel="" ;
	
	if ( t.Rows.Count > 0 )
	{
		foreach ( DataRow row in t.Rows )
		{
			string sqlSelectList2 = " select 1 from PDHRFBItem where PID=" + row["ID"].ToString() ;
			
			FBItemBO fbibo2 = new FBItemBO();
			DataTable t2 = fbibo2.GetFBItemIDAndName( sqlSelectList2 ) ;
			if ( t2.Rows.Count > 0 )
				continue;
			
			sb.Append("<childTree>");
			sb.Append("<FBItemID>"+row["ID"].ToString()+"</FBItemID>");
			sb.Append("<FBItemName>"+curLevel+"["+row["Code"].ToString()+"]"+row["Name"].ToString()+"</FBItemName>");
			sb.Append("</childTree>");
		//	sb.Append(GetSubItem(curLevel,row["ID"].ToString()));
		}
	}

	sb.Append("</data>");
	sb.Append("</xml>");
	
	
	string str = sb.ToString();
	XmlDocument oDoc = new XmlDocument();
	oDoc.LoadXml(str);

	Response.ContentType="text/xml";
	oDoc.Save(Response.OutputStream);
}

private string GetSubItem(string level, string PID)
{
	string sqlSelectList = " select FIID as [ID], FIName as [Name] from PDHRFBItem where PID=" + PID ;
	
	System.Text.StringBuilder sb = new System.Text.StringBuilder();
	
	FBItemBO fbibo = new FBItemBO();
	DataTable t = fbibo.GetFBItemIDAndName( sqlSelectList ) ;
	
	string curLevel=level + "---" ;

	if ( t.Rows.Count > 0 )
	{
		foreach ( DataRow row in t.Rows )
		{
			sb.Append("<childTree>");
			sb.Append("<FBItemID>"+row["ID"].ToString()+"</FBItemID>");
			sb.Append("<FBItemName>"+curLevel+row["Name"].ToString()+"</FBItemName>");
			sb.Append("</childTree>");
			sb.Append(GetSubItem(curLevel,row["ID"].ToString()));
		}
	}
	
	return sb.ToString() ;
}
</script>